package com.dicsystems.myrec;

import java.util.Calendar;

import com.dicsystems.myrec.dao.ImageDao;
import com.dicsystems.myrec.dao.TodoDao;
import com.dicsystems.myrec.dto.Todo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Gallery;
import android.widget.TextView;

public class DetailsActivity extends Activity {

	private Context me = this;
	private TodoDao td = new TodoDao(me);
	private ImageDao id = new ImageDao(me);
	private Calendar date;
	private Todo todo;
	private TextView start_date;
	private TextView start_time;
	private TextView end_date;
	private TextView end_time;
	private TextView title;
	private TextView category;
	private TextView memo;
	private TextView place;
	private Gallery gallery;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.details);
		date = (Calendar)getIntent().getSerializableExtra("date");
		todo = (Todo)getIntent().getSerializableExtra("todo");
		start_date = (TextView)findViewById(R.id.start_date);
		start_time = (TextView)findViewById(R.id.start_time);
		end_date = (TextView)findViewById(R.id.end_date);
		end_time = (TextView)findViewById(R.id.end_time);
		title = (TextView)findViewById(R.id.title);
		category = (TextView)findViewById(R.id.category);
		memo = (TextView)findViewById(R.id.memo);
		place = (TextView)findViewById(R.id.place);
		gallery = (Gallery)findViewById(R.id.gallery);
	}

	@Override
	protected void onResume() {
		super.onResume();
		td.open();
		id.open();
		start_date.setText(todo.start_date);
		start_time.setText(todo.start_time);
		end_date.setText(todo.end_date);
		end_time.setText(todo.end_time);
		title.setText(todo.title);
		category.setText(todo.category_name);
		memo.setText(todo.memo);
		place.setText(todo.place);
		gallery.setAdapter(new ImageAdapter(me, R.layout.image, id.select(todo)));
	}

	@Override
	protected void onPause() {
		super.onPause();
		td.close();
		id.close();
	}

	public void edit(View view) {
		Intent intent = new Intent(me, EditActivity.class);
		intent.putExtra("date", date);
		intent.putExtra("todo", todo);
		startActivity(intent);
	}

	public void delete(View view) {
		td.delete(todo);
		id.delete(todo);
		back(view);
	}

	public void back(View view) {
		if(date == null) {
			startActivity(new Intent(me, HomeActivity.class).putExtra("index", 1));
		}
		else {
			startActivity(new Intent(me, DailyListActivity.class).putExtra("date", date));
		}
	}
}
